package org.arkeologgr.appening.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import org.dom4j.XPath;

public class PresItem {

	private static final String CC_URL_PREFIX = "http://creativecommons.org/licenses/";
	private static final String KSAMSOK_URL_PREFIX = "http://kulturarvsdata.se/resurser/License#";
	private static Map<String, String> namespaces = new HashMap<String, String>();
	
	static {
		namespaces.put("pres", "http://kulturarvsdata.se/presentation#");
		namespaces.put("gml", "http://www.opengis.net/gml");
		namespaces.put("georss", "http://www.georss.org/georss");
		
	}
	
	protected Node node;

	public PresItem(Node node) {
		this.node = node;
	}

	public String getPresBuildDate() {
		return xPathHelper("pres:item/pres:buildDate");
	}

	public String getPresByline() {
		return xPathHelper("pres:item/pres:image/pres:byline");
	}

	public String getPresCoordinates() {
		return xPathHelper("pres:item/georss:where/gml:Point/gml:coordinates");
	}

	public String getPresCopyright() {
		return xPathHelper("pres:item/pres:image/pres:copyright");
	}

	public String getPresDescription() {
		return xPathHelper("pres:item/pres:description");
	}

	public String getPresEntityUri() {
		return xPathHelper("pres:item/pres:entityUri");
	}

	public String getPresIdLabel() {
		return xPathHelper("pres:item/pres:idLabel");
	}

	public String getPresItemLabel() {
		return xPathHelper("pres:item/pres:itemLabel");
	}

	public String getPresLowRes() {
		return StringUtils.trimToNull(xPathHelper("pres:item/pres:image/pres:src[@type='lowres']"));
	}

	public String getPresMediaLicense() {
		String mediaLicense = xPathHelper("pres:item/pres:image/pres:mediaLicense");
		if(mediaLicense != null) {
			if(mediaLicense.startsWith(CC_URL_PREFIX)) {
				mediaLicense = StringUtils.substringBetween(mediaLicense, CC_URL_PREFIX, "/");
			}else if(mediaLicense.startsWith(KSAMSOK_URL_PREFIX)) {
				mediaLicense = StringUtils.substringAfter(mediaLicense, KSAMSOK_URL_PREFIX);
			}
		}
		return mediaLicense;
	}
	
	public String getPresMediaLicenseUrl() {
		return xPathHelper("pres:item/pres:image/pres:mediaLicenseUrl");
	}

	public String getPresNameLabel() {
		return xPathHelper("pres:item/pres:context/pres:nameLabel");
	}

	public String getPresOrganization() {
		return xPathHelper("pres:item/pres:organization");
	}

	public String getPresPlaceLabel() {
		return xPathHelper("pres:item/pres:context/pres:placeLabel");
	}

	public String getPresRepresentationHtml() {
		return xPathHelper("pres:item/pres:representations/pres:representation[@format='HTML']");
	}

	public String getPresRepresentationRdf() {
		return xPathHelper("pres:item/pres:representations/pres:representation[@format='RDF']");
	}

	public String getPresRepresentationXml() {
		String s = xPathHelper("pres:item/pres:representations/pres:representation[@format='presentation']");
		/*
		 * Try "non standard" variants
		 */
		if (StringUtils.isEmpty(s)) {
			s = xPathHelper("pres:item/pres:representations/pres:representation[@format='Presentation']");
		}
		if (StringUtils.isEmpty(s)) {
			s = xPathHelper("pres:item/pres:representations/pres:representation[@format='XML']");
		}
		return s;
	}

	public String getPresThumbnail() {
		return xPathHelper("pres:item/pres:image/pres:src[@type='thumbnail']");
	}

	public String getPresTimeLabel() {
		String presTimeLabel = xPathHelper("pres:item/pres:context/pres:timeLabel");
		if(presTimeLabel == null) {
			return "";
		}
		String[] dateStrings = presTimeLabel.split(" - ");
		if(dateStrings.length > 1) {
			if(dateStrings[0].equals(dateStrings[1])) {
				presTimeLabel = dateStrings[0];
			}
		}
		return presTimeLabel;
	}

	public String getPresType() {
		return xPathHelper("pres:item/pres:type");
	}

	@SuppressWarnings("unchecked")
	public String xPathHelper(String path) {
		XPath xPath = DocumentHelper.createXPath(path);
		xPath.setNamespaceURIs(namespaces);
		List<Node> nodes = xPath.selectNodes(node);
		
		if (!nodes.isEmpty()) {
			return nodes.get(0).getStringValue().trim();
		} else {
			return null;
		}
	}

	
	@SuppressWarnings("unchecked")
	public static List<Node> getItems(Document document) {
		XPath xpath = DocumentHelper.createXPath("//pres:item/..");
		xpath.setNamespaceURIs(namespaces);
		
		return xpath.selectNodes(document.getRootElement());
	}
	
}
